import { axios } from '@/service/service'
import { api_dolphin, api_user, api_resource } from '@/service/module.config'

import type {
  UserNameReq,
  UserNamesReq,
  AlertGroupIdReq,
  UserReq,
  IdReq,
  IdStringReq,
  UserIdReq,
  GrantDataSourceReq,
  GrantResourceReq,
  GrantProject,
  ProjectCodeReq,
  GrantUDFReq,
  GrantNamespaceReq,
  ListAllReq,
  ListReq,
  RegisterUserReq
} from './types'

export function activateUser(data: UserNameReq): any {
  return axios({
    url: `${api_dolphin}/users/activate`,
    method: 'post',
    data
  })
}

export function authorizedUser(params: AlertGroupIdReq): any {
  return axios({
    url: `${api_dolphin}/users/authed-user`,
    method: 'get',
    params
  })
}

export function batchActivateUser(data: UserNamesReq): any {
  return axios({
    url: `${api_dolphin}/users/batch/activate`,
    method: 'post',
    data
  })
}

export function createUser(data: UserReq): any {
  return axios({
    url: `${api_dolphin}/users/create`,
    method: 'post',
    data
  })
}

export function delUserById(data: IdReq) {
  return axios({
    url: `${api_dolphin}/users/delete`,
    method: 'post',
    data
  })
}

export function getUserInfo(): any {
  return axios({
    url: `${api_dolphin}/users/get-user-info`,
    method: 'get'
  })
}

export function grantDataSource(data: GrantDataSourceReq) {
  return axios({
    url: `${api_dolphin}/users/grant-datasource`,
    method: 'post',
    data
  })
}

export function grantResource(data: GrantResourceReq) {
  return axios({
    url: `${api_dolphin}/users/grant-file`,
    method: 'post',
    data
  })
}

export function grantProject(data: GrantProject) {
  return axios({
    url: `${api_dolphin}/users/grant-project`,
    method: 'post',
    data
  })
}

export function grantProjectByCode(data: ProjectCodeReq & UserIdReq): any {
  return axios({
    url: `${api_dolphin}/users/grant-project-by-code`,
    method: 'post',
    data
  })
}

export function grantUDFFunc(data: GrantUDFReq & UserIdReq) {
  return axios({
    url: `${api_dolphin}/users/grant-udf-func`,
    method: 'post',
    data
  })
}

export function grantNamespaceFunc(data: GrantNamespaceReq & UserIdReq) {
  return axios({
    url: `${api_dolphin}/users/grant-namespace`,
    method: 'post',
    data
  })
}

export function listUser(): any {
  return axios({
    url: `${api_dolphin}/users/list`,
    method: 'get'
  })
}

export function listAll(params?: ListAllReq): any {
  return axios({
    url: `${api_dolphin}/users/list-all`,
    method: 'get',
    params
  })
}

export function queryUserList(params: ListReq): any {
  return axios({
    url: `${api_dolphin}/users/list-paging`,
    method: 'get',
    params
  })
}

export function registerUser(data: RegisterUserReq): any {
  return axios({
    url: `${api_dolphin}/users/register`,
    method: 'post',
    data
  })
}

export function revokeProject(data: ProjectCodeReq & UserIdReq): any {
  return axios({
    url: `${api_dolphin}/users/revoke-project`,
    method: 'post',
    data
  })
}

export function unauthorizedUser(params: AlertGroupIdReq): any {
  return axios({
    url: `${api_dolphin}/users/unauth-user`,
    method: 'get',
    params
  })
}

export function updateUser(data: IdStringReq & UserReq) {
  return axios({
    url: `${api_dolphin}/users/update`,
    method: 'post',
    data
  })
}

export function verifyUserName(params: UserNameReq) {
  return axios({
    url: `${api_dolphin}/users/verify-user-name`,
    method: 'get',
    params
  })
}

export function apiGetUserInfo(): any {
  return axios({
    url: `${api_user}/info`,
    method: 'get'
  })
}

export function apiUpdateUser(data: IdStringReq & UserReq) {
  return axios({
    url: `${api_user}/update-info`,
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: JSON.stringify(data)
  })
}

export function apiUploadFile(data: any) {
  return axios({
    url: `${api_resource}/oss/endpoint/put-file`,
    method: 'post',
    data
  })
}

export function apiUpdatePassword(data: UserReq) {
  return axios({
    url: `${api_user}/update-password`,
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    params: data
  })
}
